﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections;
using QnA;

/// <summary>
/// Summary description for CreateXMLCategory
/// </summary>
public partial class CreateXMLCategory : System.Web.UI.Page
{
    ProcessUser processUser;
    DataView dvRoot;
    DataView dvChild;

	public CreateXMLCategory()
	{
	}

    public void WriteXML_top7()
    {
        XmlTextWriter writer;
        try
        {
            
            processUser = new ProcessUser();
            processUser.selectRootCategory();
            dvRoot = ProcessUser.dataset.Tables["RootCategory"].DefaultView;
            string fileName = Server.MapPath("Data/category_top7.xml");
            writer = new XmlTextWriter(fileName, System.Text.Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 6;
            writer.WriteStartDocument(); 
            writer.WriteStartElement("MenuItems");
           
            for (int i = 0; i < 7; i++)
            {                   
                writer.WriteStartElement("RootItem");
                writer.WriteAttributeString("Text", dvRoot[i]["CatName"].ToString());
                writer.WriteAttributeString("NavigateUrl", "~/User/ShowExpert.aspx?id=" + dvRoot[i]["CatID"].ToString());

                processUser.selectChildCategory(dvRoot[i]["CatID"].ToString(), "ChildCategory");
                dvChild = ProcessUser.dataset.Tables["ChildCategory"].DefaultView;
                for (int t = 0; t < dvChild.Count; t++)
                {
                    writer.WriteStartElement("SubItem");
                    writer.WriteAttributeString("Text", dvChild[t]["CatName"].ToString());
                    writer.WriteAttributeString("NavigateUrl", "~/User/ShowExpert.aspx?id=" + dvChild[t]["CatID"].ToString());

                    processUser.selectChildCategory(dvChild[t]["CatID"].ToString(), "ChildCategory1");
                    DataView dvChild1 = ProcessUser.dataset.Tables["ChildCategory1"].DefaultView;
                    for (int u = 0; u < dvChild1.Count; u++)
                    {
                        writer.WriteStartElement("SubItem");
                        writer.WriteAttributeString("Text", dvChild1[u]["CatName"].ToString());
                        writer.WriteAttributeString("NavigateUrl", "~/User/ShowExpert.aspx?id=" + dvChild1[u]["CatID"].ToString());
                        writer.WriteEndElement();

                    }
                    writer.WriteEndElement();
                }            
                writer.WriteEndElement();
            }
            writer.WriteStartElement("RootItem");
            writer.WriteAttributeString("Text", "Tất cả");
            writer.WriteAttributeString("NavigateUrl", "~/User/ShowCategory.aspx");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
            writer.Close();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
            //Response.Write(ex.ToString());
        }
      
    }

    public void WriteXML_All()
    {
        XmlTextWriter writer;
        try
        {
            processUser = new ProcessUser();
            processUser.selectRootCategory();
            dvRoot = ProcessUser.dataset.Tables["RootCategory"].DefaultView;
            string fileName = Server.MapPath("Data/category_all.xml");
            writer = new XmlTextWriter(fileName, System.Text.Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 6;
            writer.WriteStartDocument(true);
            writer.WriteStartElement("TREENODES");

            for (int i = 0; i < dvRoot.Count; i++)
            {
                writer.WriteStartElement("RootNode");
                writer.WriteAttributeString("Text", dvRoot[i]["CatName"].ToString() + "("+dvRoot[i]["TotalExpert"].ToString() +")");
                writer.WriteAttributeString("NavigateUrl", "~/User/ShowExpert.aspx?id=" + dvRoot[i]["CatID"].ToString());
                writer.WriteAttributeString("Value", dvRoot[i]["CatID"].ToString());

                processUser.selectChildCategory(dvRoot[i]["CatID"].ToString(), "ChildCategory");
                dvChild = ProcessUser.dataset.Tables["ChildCategory"].DefaultView;
                for (int t = 0; t < dvChild.Count; t++)
                {
                    writer.WriteStartElement("SubNode");
                    writer.WriteAttributeString("Text", dvChild[t]["CatName"].ToString()+ "("+dvChild[t]["TotalExpert"].ToString() +")");
                    writer.WriteAttributeString("NavigateUrl", "~/User/ShowExpert.aspx?id=" + dvChild[t]["CatID"].ToString());
                    writer.WriteAttributeString("Value", dvChild[t]["CatID"].ToString());

                    processUser.selectChildCategory(dvChild[t]["CatID"].ToString(), "ChildCategory1");
                    DataView dvChild1 = ProcessUser.dataset.Tables["ChildCategory1"].DefaultView;
                    for (int u = 0; u < dvChild1.Count; u++)
                    {
                        writer.WriteStartElement("SubNode");
                        writer.WriteAttributeString("Text", dvChild1[u]["CatName"].ToString() + "(" + dvChild1[u]["TotalExpert"].ToString() + ")");
                        writer.WriteAttributeString("NavigateUrl", "~/User/ShowExpert.aspx?id=" + dvChild1[u]["CatID"].ToString());
                        writer.WriteAttributeString("Value", dvChild1[u]["CatID"].ToString());
                        writer.WriteEndElement();

                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.Flush();
            writer.Close();
        }
        catch (Exception ex)
        {
            //Response.Write(ex.ToString());
            HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=writeXML");
        }

    }

}
